package com.faya.green.controller.main;

import com.faya.green.enumeration.ResultEnum;
import com.faya.green.model.BaseInfo;
import com.faya.green.model.ResultJson;
import com.faya.green.service.BaseInfoService;
import com.faya.green.utils.CommonUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户 ：LX
 * 创建时间： 2018/7/23. 13:46
 * 地点：广州
 * 目的: 基础信息控制层
 * 结果：
 */
@Controller
@RequestMapping("/base")
public class BaseInfoController {

    @Autowired
    private BaseInfoService baseInfoService;


    /**
     * 添加基础信息
     * @param baseInfo 基础信息
     * @return
     */
    @RequestMapping(value = "/addBaseInfo.php", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("admin:sys.base:add")
    @RequiresRoles("admin")
    public ResultJson addBaseInfo(BaseInfo baseInfo){
        ResultJson resultJson = new ResultJson();
        if (baseInfo == null){
            resultJson.setMsg("请输入基础信息");
            resultJson.setStatus(ResultEnum.RESULT_ERROR.getStatus());
            return resultJson;
        }

        try {
            baseInfoService.addBaseInfo(baseInfo);
            resultJson.setMsg("添加成功");
            resultJson.setStatus(ResultEnum.RESULT_SUCCESS.getStatus());
            return resultJson;
        } catch (Exception e) {
            e.printStackTrace();
            resultJson.setMsg("添加失败");
            resultJson.setStatus(ResultEnum.RESULT_ERROR.getStatus());
            return resultJson;
        }
    }

    /**
     * 动态获取基础数据
     * @param labelEn 分类的英文名称
     * @return 动态的json数据
     */
    @RequestMapping(value = "/getMovingData.php", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("admin:sys.base:query")
    @RequiresRoles("admin")
    public ResultJson getMovingData(String labelEn){
        ResultJson resultJson = new ResultJson();
        if (CommonUtils.checkNull(labelEn)){
            resultJson.setMsg("未传递必要的参数");
            resultJson.setStatus(ResultEnum.RESULT_ERROR.getStatus());
            return resultJson;
        }

        try {
            List<BaseInfo> infos = baseInfoService.getMovingData(labelEn);
            resultJson.setMsg("查询数据成功");
            resultJson.setStatus(ResultEnum.RESULT_SUCCESS.getStatus());
            resultJson.setObject(infos);
            return resultJson;
        } catch (Exception e) {
            e.printStackTrace();
            resultJson.setMsg("查询异常");
            resultJson.setStatus(ResultEnum.RESULT_ERROR.getStatus());
            return resultJson;
        }
    }
}
